package com.whiteape.book.utils;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.google.gson.TypeAdapter;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GsonUtils {
    private static Gson gson = null;

    static {
        if (gson == null) {
            GsonBuilder gsonBuilder = new GsonBuilder();
            gsonBuilder.setDateFormat("yyyy-MM-dd");//设置时间格式
            gsonBuilder.registerTypeAdapter(String.class, new StringNullAdapter());

            gson = gsonBuilder.create();
        }
    }

    private GsonUtils() {
    }

    /**
     * 转成json
     *
     * @param object
     * @return
     */
    public static String obj2String(Object object) {
        String gsonString = null;
        if (gson != null) {
            gsonString = gson.toJson(object);
        }
        return gsonString;
    }

    /**
     * 转成bean
     *
     * @param gsonString
     * @param cls
     * @return
     */
    public static <T> T string2Obj(String gsonString, Class<T> cls) {
        T t = null;
        if (gson != null) {
            t = gson.fromJson(gsonString, cls);
        }
        return t;
    }

    /**
     * 转成list
     *
     * @param gsonString
     * @param cls
     * @return
     */
    public static <T> List<T> string2List(String gsonString, Class<T> cls) {
//        if (gson != null) {
//            list = gson.fromJson(gsonString, new TypeToken<List<T>>() {
//            }.getType());
        List<T> list = new ArrayList<T>();
        try {
            JsonArray array = new JsonParser().parse(gsonString).getAsJsonArray();
            for (final JsonElement elem : array) {
                list.add(gson.fromJson(elem, cls));
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
//        }
        return list;
    }


    public static <T> ArrayList<T> string2ArrayList(String gsonString, Class<T> cls) {
        ArrayList<T> list = null;
        if (gson != null) {
            list = gson.fromJson(gsonString, new TypeToken<ArrayList<T>>() {
            }.getType());
        }
        return list;
    }

    /**
     * 转成list中有map的
     *
     * @param gsonString
     * @return
     */
    public static <T> List<Map<String, T>> string2ListMaps(String gsonString) {
        List<Map<String, T>> list = null;
        if (gson != null) {
            list = gson.fromJson(gsonString,
                    new TypeToken<List<Map<String, T>>>() {
                    }.getType());
        }
        return list;
    }

    /**
     * 转成map的
     *
     * @param gsonString
     * @return
     */
    public static <T> Map<String, T> string2Maps(String gsonString) {
        Map<String, T> map = null;
        if (gson != null) {
            map = gson.fromJson(gsonString, new TypeToken<Map<String, T>>() {
            }.getType());
        }
        return map;
    }

    /**
     * 转成hashmap的
     *
     * @param gsonString
     * @return
     */
    public static <T> HashMap<String, T> string2HashMaps(String gsonString) {
        HashMap<String, T> map = null;
        if (gson != null) {
            map = gson.fromJson(gsonString, new TypeToken<HashMap<String, T>>() {
            }.getType());
        }
        return map;
    }
    public static class StringNullAdapter extends TypeAdapter<String> {


        @Override
        public void write(JsonWriter jsonWriter, String s) throws IOException {
            if (s == null) {//序列化使用的是adapter的write方法
                //jsonWriter.nullValue();//这个方法是错的，而是应该将null转成""
                jsonWriter.value("");
                return;
            }
            jsonWriter.value(s);
        }

        @Override
        public String read(JsonReader jsonReader) throws IOException {
            if (jsonReader.peek() == JsonToken.NULL) {//反序列化使用的是read方法
                jsonReader.nextNull();
                return "";
            }
            return jsonReader.nextString();
        }
    }
}
